package cn.two.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.two.model.OaNews;
import cn.two.serviceImpl.NewsService;



@Controller

public class NewsController {

	@Resource(name = "newsService")
	private NewsService newsService;

	public NewsService getNewsService() {
		return newsService;
	}

	public void setNewsService(NewsService newsService) {
		this.newsService = newsService;
	}
	
	
	
	 @InitBinder  
	 public void initBinder(WebDataBinder binder) {  
	 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
	 dateFormat.setLenient(false);  
	 binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	 }
	
	
	
	
	

	

	@RequestMapping("/newslist")
	private String getAllNews(OaNews oaNews,HttpSession session){		
		List<OaNews> news = newsService.getNewsByPage(oaNews);
		session.setAttribute("totalnews", news);
		return "NewFile";
	}

	
	@ResponseBody
	@RequestMapping("/list")
	private List<OaNews> getNews( OaNews oaNews,HttpSession session){		
		
		List<OaNews> news = newsService.getNewsByPage(oaNews);
		
		return news;
	}

	
	@ResponseBody
	@RequestMapping("/getFinalPages")
	public List getFinalPages(OaNews oaNews,HttpSession session) {
		
		List list = new ArrayList();
		  int count;
		  int pages;
		  if(oaNews.getFlag()!=0){
			  count=newsService.getTotalCount();  			 
		  }else{
			  
			  count=newsService.getTotalCountByJointQuery(oaNews);
			  
		  }
		  
	     		
		 
				 
	
		
		if (count >= oaNews.getPsize()) {			
			pages = newsService.getTotalPages(count, oaNews.getPsize());
		} else {			
			pages = 1;
		}
		
		list.add(pages);
		list.add(count);
		return list;
	}
	
	 
}
